﻿using System.Collections.ObjectModel;
using System.Windows;
using System;
using System.Windows.Controls;

namespace BlankApp.Views
{
    /// <summary>
    /// Interaction logic for WordGroupControl
    /// </summary>
    public partial class WordGroupControl : UserControl
    {
        public ObservableCollection<WordItem> Words { get; set; } = new ObservableCollection<WordItem>();

        public WordGroupControl()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public static readonly DependencyProperty CharacterProperty =
            DependencyProperty.Register("Character", typeof(string), typeof(WordGroupControl),
                new PropertyMetadata("", OnCharacterChanged));

        public static readonly DependencyProperty PinyinProperty =
            DependencyProperty.Register("Pinyin", typeof(string), typeof(WordGroupControl),
                new PropertyMetadata("", OnCharacterChanged));

        public static readonly DependencyProperty ShowPinyinProperty =
            DependencyProperty.Register("ShowPinyin", typeof(bool), typeof(WordGroupControl),
                new PropertyMetadata(true, OnCharacterChanged));

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(WordGroupControl),
                new PropertyMetadata(true, OnCharacterChanged));

        public string Character
        {
            get => (string)GetValue(CharacterProperty);
            set => SetValue(CharacterProperty, value);
        }

        public string Pinyin
        {
            get => (string)GetValue(PinyinProperty);
            set => SetValue(PinyinProperty, value);
        }

        public bool ShowPinyin
        {
            get => (bool)GetValue(ShowPinyinProperty);
            set => SetValue(ShowPinyinProperty, value);
        }
        public bool IsSelected
        {
            get => (bool)GetValue(IsSelectedProperty);
            set => SetValue(IsSelectedProperty, value);
        }

        private static void OnCharacterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is WordGroupControl control)
            {
                control.UpdateWords();
            }
        }

        private void UpdateWords()
        {
            Words.Clear();

            if (string.IsNullOrWhiteSpace(Character)) return;

            var pinyinList = (Pinyin ?? "").Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < Character.Length; i++)
            {
                Console.WriteLine($"wordgroup={Character[i].ToString()},{Pinyin},{ShowPinyin},{IsSelected}");
                Words.Add(new WordItem
                {
                    Character = Character[i].ToString(),
                    Pinyin = i < pinyinList.Length ? pinyinList[i] : "",
                    ShowPinyin = ShowPinyin,
                    IsSelected = IsSelected
                });
            }
        }
    }

    public class WordItem
    {
        public string Character { get; set; } = string.Empty;
        public string Pinyin { get; set; } = string.Empty;
        public bool ShowPinyin { get; set; }
        public bool IsSelected { get; set; }
    }
}
